package com.yunnuo.systool.formbuilder.controller;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.yunnuo.system.comm.controller.BaseController;
import com.yunnuo.system.comm.util.ResultData;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.systool.formbuilder.entity.FormBuilder;
import com.yunnuo.systool.formbuilder.service.IFormBuilderService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import java.util.List;

/**
 * Created by w_weihf on 2018/9/25.
 */
@Controller
@RequestMapping("/systool/formbuilder")
public class FromBuilderController extends BaseController {

    private String prefix = "/systool/formbuilder";

    @Autowired
    private IFormBuilderService formBuilderService;

    @RequiresPermissions("systool:formbuilder:view")
    @GetMapping()
    public String frommodel()
    {
        return prefix + "/formdemo";
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list()
    {
        //前台分页
        startPage();
        List<FormBuilder> modelList = formBuilderService.findList();
        return getDataTable(modelList);
    }
    /**
     * 分页查询
     * @return
     */
    @GetMapping("/jsonlist")
    @ResponseBody
    public  JSON jsonlist()
    {

        List<FormBuilder> modelList = formBuilderService.findList();
        JSONObject jsonObject =new JSONObject();
        jsonObject.accumulate("message","");
        jsonObject.accumulate("value", JSONArray.fromObject(modelList));
        return jsonObject;
    }
    /**
     * 保存
     */
    @PostMapping("/save")
    @ResponseBody
    public JSON save(ServletRequest request)
    {
        ResultData data = new ResultData();
        try{
            String name =request.getParameter("name");
            String html =request.getParameter("html");
            FormBuilder f =new FormBuilder();
            f.setDemoName(name);
            f.setDemoValue(html);
            this.formBuilderService.addVO(f);
            data.setStat(ResultData.STAT_SUCCESS);
            data.setMessage("操作成功！");
        }catch (Exception e){
            e.printStackTrace();
            data.setStat(ResultData.STAT_FAILURE);
            data.setMessage("操作失败！");
        }

        return data.toJson();
    }
    /**
     * 新增
     */
    @RequiresPermissions("systool:formbuilder:add")
    @GetMapping("/add")
    public String add( org.springframework.ui.Model model)
    {
        return prefix + "/add";
    }
    /**
     * Demo
     */
    @GetMapping("/model/{id}")
    public String model( @PathVariable("id") String  id , org.springframework.ui.Model model)
    {
        FormBuilder fb =   this.formBuilderService.findVO(id);
        model.addAttribute("formhtml", fb.getDemoValue());
        return prefix + "/model";
    }
    /**
     * 删除
     */
    @RequestMapping("/remove/{id}")
    @ResponseBody
    public JSON remove(@PathVariable("id") String  id, org.springframework.ui.Model model)
    {
        ResultData data = new ResultData();
        formBuilderService.deleteVo(id);
        data.setStat(ResultData.STAT_SUCCESS);
        data.setMessage("操作成功！");
        return data.toJson();
    }
}
